//
//  MBShowCell.m
//  mibaotu
//
//  Created by 彭鹏 on 16/9/17.
//  Copyright © 2016年 pegnk. All rights reserved.
//

#import "MBShowCell.h"
#import "UIButton+WebCache.h"

@interface MBShowCell ()

@property (weak, nonatomic) IBOutlet UIImageView *zanImgView;
@property (weak, nonatomic) IBOutlet UILabel *zanTitleLb;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *zanWidthLayout;

@end
@implementation MBShowCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identify = @"MBShowCell";
    MBShowCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MBShowCell" owner:self options:nil] lastObject];
    }
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)onUpdateZanBtAction:(BOOL)isZan
{
    _isZan = isZan;
    if (_isZan) {
        _zanTitleLb.textColor = TINK_COLOR;
        _zanTitleLb.font = [UIFont systemFontOfSize:13.f];
        _zanImgView.image = [UIImage imageNamed:@"zan2-1"];
    } else {
        _zanTitleLb.textColor = UIColorFromHex(0x333333);
        _zanTitleLb.font = [UIFont systemFontOfSize:13.f];
        _zanImgView.image = [UIImage imageNamed:@"zan1-1"];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (IBAction)onSelectReplyAction {
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectIconBtnWithView:withDataDic:)]) {
        [_delegate onSelectIconBtnWithView:self withDataDic:_dateDic];
    }
}

- (IBAction)onSelectPraiseAction {
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectPraiseBtnWithView:withValue:)]) {
        [_delegate onSelectPraiseBtnWithView:self withValue:_dateDic];
    }
    
}

- (void)setDateDic:(NSDictionary *)dateDic
{
    if (!_dateDic) {
        _dateDic = [[NSDictionary alloc]init];
    }
    _dateDic = dateDic;
    [self setNeedsLayout];
}

- (void)setIsZan:(BOOL)isZan
{
    _isZan = isZan;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (!_dateDic) return;
    
    NSString *faceImg = [_dateDic objectForKey:@"FaceImg"];
    if (![faceImg isKindOfClass:[NSNull class]]) {
        [_iconBtn sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,faceImg]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"xiaobian_bg"] options:SDWebImageRetryFailed];
    }
    _iconBtn.layer.cornerRadius = _iconBtn.height/2.;
    _iconBtn.layer.masksToBounds = YES;
    _title.text = [NSString stringWithFormat:@"%@",[[_dateDic objectForKey:@"NickName"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    _zanTitleLb.right = _zan.right;
    _zanTitleLb.text = [NSString stringWithFormat:@"赞(%@)",[[NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"PraiseNum"]] getValues]];
    float zanWidth = [_zanTitleLb.text boundingRectWithSize:CGSizeMake(1000, _zanTitleLb.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_zanTitleLb.font} context:nil].size.width+5;
    _zanWidthLayout.constant = zanWidth;
    if (_isZan) {
        _zanTitleLb.textColor = TINK_COLOR;
        _zanTitleLb.font = [UIFont systemFontOfSize:13.f];

        _zanImgView.image = [UIImage imageNamed:@"zan2-1"];
    } else {
        _zanTitleLb.textColor = UIColorFromHex(0x333333);
        _zanTitleLb.font = [UIFont systemFontOfSize:13.f];

        _zanImgView.image = [UIImage imageNamed:@"zan1-1"];
    }
    
    NSString *coverPath  = [_dateDic objectForKey:@"CoverPath"];
    if (![coverPath isKindOfClass:[NSNull class]]) {
        [_bigImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,coverPath]] placeholderImage:[UIImage imageNamed:@"show_qzx_bg"] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _bigImageView.image = [self getCenterImg:image];
        }];
    }
//    _bigImageView.contentMode = UIViewContentModeScaleAspectFit;
    _themeTitle.text = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"ShowTitle"]];
    
    NSString *showType = [_dateDic objectForKey:@"ShowTitle"];
    if ([showType intValue] == 1) {
         [_show setTitle:@"宝贝秀" forState:UIControlStateNormal];
    }
    else if ([showType intValue] == 2) {
        [_show setTitle:@"主播秀" forState:UIControlStateNormal];
    } else {
        [_show setTitle:@"主播秀" forState:UIControlStateNormal];
    }
    
}

- (UIImage *)getCenterImg:(UIImage *)image
{
    CGImageRef imageRef = image.CGImage;
    float imgHeight = 1.0 * image.size.width * _bigImageView.height/_bigImageView.width;
    float rectHeight = imgHeight;
    float top = (image.size.height-imgHeight)/2.;
    CGRect rect;
    if (top > 0) {
        //图片实际高度比UI高度高
        if (imgHeight > image.size.height) {
            rectHeight = image.size.height;
            top = (imgHeight - image.size.height)/2.;
        }
        rect = CGRectMake(0, top, image.size.width, rectHeight);
    }
    else {
        //UI高度比图片实际高度高
        float imgWidth = 1.0 * image.size.height * _bigImageView.width/_bigImageView.height;
        float rectWidth = imgWidth;
        float left = (image.size.width-imgWidth)/2.;
        if (imgWidth > image.size.width) {
            rectWidth = image.size.width;
            left = (imgWidth - image.size.width)/2.;
        }
        rect = CGRectMake(left, 0, rectWidth, image.size.height);
    }
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    return rectImg;
}

@end
